cmake_minimum_required (VERSION 3.8)
project(LibGridTIFF)
set(CMAKE_CXX_STANDARD 17)
if (MSVC)
    add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
endif ()

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(DGGS_OUT_DIR ${CMAKE_OUT_DIR}/out/build/debug)
else ()
    set(DGGS_OUT_DIR ${CMAKE_OUT_DIR}/out/build/release)
endif ()

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${DGGS_OUT_DIR}/LibGridTIFF)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${DGGS_OUT_DIR}/LibGridTIFF)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${DGGS_OUT_DIR}/LibGridTIFF)

set(CMAKE_DEBUG_POSTFIX D)
set(CMAKE_RELEASE_POSTFIX )

include_directories("${CMAKE_SOURCE_DIR}/LibDGGSBase")

if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
    message(STATUS "Configuring for Linux")
    find_package(GDAL REQUIRED)
    include_directories(${GDAL_INCLUDE_DIRS})
elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
    message(STATUS "Configuring for Windows")
    include_directories(${CMAKE_SOURCE_DIR}/3rdlibs/include)
    link_directories(${CMAKE_SOURCE_DIR}/3rdlibs/lib)
endif()

set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

file(GLOB_RECURSE SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
add_library(LibGridTIFF SHARED ${SOURCE_FILES})

# DLL
target_compile_definitions(LibGridTIFF PRIVATE BUILDING_DLL)

if(ISEXTRACT)
    file(GLOB_RECURSE MyLIB_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp)
    foreach(header_file ${MyLIB_HEADERS})
        get_filename_component(header_dir ${header_file} DIRECTORY)
        string(REPLACE ${CMAKE_CURRENT_SOURCE_DIR} ${lib_output_directory}/include/LibGridTIFF output_header_dir ${header_dir})
        file(MAKE_DIRECTORY ${output_header_dir})
        configure_file(${header_file} ${output_header_dir} COPYONLY)
    endforeach()

    if (CMAKE_BUILD_TYPE STREQUAL "Debug")
        add_custom_command(TARGET LibGridTIFF POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/LibGridTIFFD.lib
                ${lib_output_directory}/lib/debug/LibGridTIFFD.lib
                COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/LibGridTIFFD.dll
                ${lib_output_directory}/bin/debug/LibGridTIFFD.dll
                COMMENT "Copying LibGridTIFF debug files..."
        )
    else ()
        add_custom_command(TARGET LibGridTIFF POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/LibGridTIFF.lib
                ${lib_output_directory}/lib/release/LibGridTIFF.lib
                COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/LibGridTIFF.dll
                ${lib_output_directory}/bin/release/LibGridTIFF.dll
                COMMENT "Copying LibGridTIFF release files..."
        )
    endif ()
endif()

install(TARGETS LibGridTIFF
        LIBRARY DESTINATION ${lib_install_directory}/lib
        ARCHIVE DESTINATION ${lib_install_directory}/lib
        RUNTIME DESTINATION ${lib_install_directory}/bin
        PUBLIC_HEADER DESTINATION ${lib_install_directory}/include
)
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/" # source directory
        DESTINATION ${lib_install_directory}/include # target directory
        FILES_MATCHING # install only matched files
        PATTERN "*.hpp" # select header files
)

if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
    target_include_directories(LibGridTIFF PRIVATE ${GDAL_INCLUDE_DIRS})
    target_link_libraries(LibGridTIFF PRIVATE ${GDAL_LIBRARIES})
elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
    target_link_libraries(LibGridTIFF gdal_i)
endif()
